/*
	Author: Sidek Sirun
	Course: UCI Java Programming I (X460.10)
	Assignment 2: 5 Person Objects

		Write a Java program that creates five objects of the Person class you created in Lesson 2. 
		The program should update key data fields and print those values back out to the screen. In 
		addition, your program should implement a getOldestPerson() method that returns the full 
		name of the oldest person.

		Hint 1: You will need to create a separate program that contains a main method in order to 
		create Person objects. You use the "javac" command to compile your code and the "java" 
		command to execute your code and see the output.

		Hint 2: In order to set the key data fields, such as, first name, last name, age, etc., you 
		must add a constructor or "setter" methods to your Person class. An example setter method 
		would be, setFirstName(…).
*/

class PersonTest {

	/**
	* getOldestPerson() method
	*/
	public static void getOldestPerson(Person[] personArray) {		
		Person[] pa         = personArray;
		int oldestAge       = 0;
		int count           = 0;
		String oldestPerson = "";
		
		// find oldest age
		for (int i = 0; i < pa.length; i++) {
			if (oldestAge < pa[i].getAge()) {
				oldestAge    = pa[i].getAge();
				//oldestPerson = pa[i].getFullName();
			}
		}

		// find oldest person (if there are more than one) once the oldest age is known
		for (int j = 0; j < pa.length; j++) {
			if (oldestAge == pa[j].getAge()) {
				count++;
				if (count == 1) {
					oldestPerson = pa[j].getFullName();
				} else {
					oldestPerson = oldestPerson + ", " + pa[j].getFullName();
				}
			}
		}

		System.out.println("Oldest age: " + oldestAge);
		System.out.println("No. of oldest person: " + count);
		if (count == 1) {
			System.out.println("Oldest person: " + oldestPerson);
		} else {
			System.out.println("Oldest persons: " + oldestPerson);
		}
	}

	/**
	* main() method
	*/
	public static void main(String[] args) {
		// New instances of Person class
		Person[] personArray   = new Person [] {
								 new Person("James",  "Bond",  49, "Male",   "011-70-7777"),
								 new Person("Lara",   "Croft", 25, "Female", "033-55-8888"),
								 new Person("Austin", "Power", 51, "Male",   "001-03-9999"),
								 new Person("Mary",   "Jane",  51, "Female", "010-05-9000"),
								 new Person("Clark",  "Kent",  39, "Male",   "100-50-1000")
								};

		// Print to STDOUT
		System.out.println("");
		System.out.println("Person toString() method:");
		for (int x = 0; x < personArray.length; x++) {
			System.out.println("Information about person " + x + ": " + personArray[x].toString());
		}
		
		System.out.println("");
		System.out.println("Person talk() method:");
		for (int y = 0; y < personArray.length; y++) {
			System.out.println(personArray[y].talk());
		}
		System.out.println("");

		System.out.println("PersonTest getOldestPerson() method:");
		getOldestPerson(personArray);
		System.out.println("");
	}

}